﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using cecbData;
using cecbClasses;
using cecbClasses.Data_Classes;
using cecbClasses.Data_Adapters;
using cecbClasses.Data_Adapters.Project;
using System.Data;

namespace CECB.Project
{
    /// <summary>
    /// Interaction logic for UpdateProj_EIADetails.xaml
    /// </summary>
    public partial class UpdateProj_EIADetails : Window
    {

        string destFile;
        string modlDestFile;

        Func_Projects ObjProjctsFunc = new Func_Projects();

        public UpdateProj_EIADetails()
        {
            InitializeComponent();
            ProjectAutocompleteTextBox.ItemsSource = ObjProjctsFunc.GetAllPendingProjNames();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            wrapPanel_SimpleCritria.Visibility = System.Windows.Visibility.Hidden;
            wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Hidden;
            wrapPanel_Importnce.Visibility = System.Windows.Visibility.Hidden;

            //FillEIAProjectReferences();
            //FillPrjType();
        }

        //public void FillEIAProjectReferences()
        //{


        //    List<Projects> ProjRefrncesList = ObjProjctsFunc.GetAllPendingProjRefernces();
        //    //comboBox_ProjRefernc.ItemsSource = ProjRefrncesList;


        //}




        //public void FillPrjType()
        //{
        //    DataAdapter ObjDataAdptr = new DataAdapter();
        //    List<cecbData.ProjectTypes> ProjectTypeList = ObjDataAdptr.GetAllProjectTypes();
        //    comboBox_ProjType.ItemsSource = ProjectTypeList; ;
        //}



        private void button_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ReferenceTextBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            Data_Projects ObjProj1 = new Data_Projects();
            ObjProj1.Proj_name = ProjectAutocompleteTextBox.Text;

            Projects selectedPrjRef1 = ObjProjctsFunc.GetPendingProjectDetailByName(ObjProj1);
            label_ProjRef.Content = selectedPrjRef1.proj_reference;



            ObjProj1.Proj_reference = label_ProjRef.Content.ToString();
            Projects selectedPrj = ObjProjctsFunc.GetPendingProjectDetailByCode(ObjProj1);

            //Data_Categories ObjProjCat = new Data_Categories();
            //ObjProjCat.Cat_reference = selectedPrj.Categ;
            //cecbData.Categories selectProjctCat = ObjProjctsFunc.Getca(ObjProjCat);

            Data_ProjectTypes ObjProjType = new Data_ProjectTypes();
            ObjProjType.Ptype_reference = selectedPrj.proj_type;
            cecbData.ProjectTypes selectProjctType = ObjProjctsFunc.GetProjectByTypeCode(ObjProjType);

            try
            {

                if (ObjProj1.Proj_reference != " ")
                {
                    // textBox_projname.Text = selectedPrj.proj_name;
                    textBox_ProjType.Text = selectProjctType.ptype_name;
                    textBox_Location.Text = selectedPrj.proj_location;
                    datePicker_FromDate.Text = selectedPrj.proj_fromDate.ToString();
                    datePicker_ToDate.Text = selectedPrj.proj_toDate.ToString();
                    destFile = selectedPrj.proj_planImage;
                    modlDestFile = selectedPrj.proj_modelImage;

                    ShowData(label_ProjRef.Content.ToString());



                    if (destFile == null && modlDestFile == null)
                    {
                        label_ModelInfo.Content = "Image not found".ToString();
                        label_PlanInfo.Content = "Image not found".ToString();
                        image_Model.Visibility = System.Windows.Visibility.Hidden;
                        image_Plan.Visibility = System.Windows.Visibility.Hidden;



                    }
                    else if (destFile == null)
                    {
                        label_ModelInfo.Content = "".ToString();
                        image_Plan.Visibility = System.Windows.Visibility.Hidden;
                        label_PlanInfo.Content = "Image not found".ToString();
                        image_Model.Visibility = System.Windows.Visibility.Visible;
                        image_Model.Source = new BitmapImage(new Uri(modlDestFile));
                    }
                    else if (modlDestFile == null)
                    {
                        label_PlanInfo.Content = "".ToString();
                        label_ModelInfo.Content = "Image not Found".ToString();
                        image_Model.Visibility = System.Windows.Visibility.Hidden;
                        image_Plan.Visibility = System.Windows.Visibility.Visible;
                        image_Plan.Source = new BitmapImage(new Uri(destFile));


                    }
                    else
                    {
                        image_Model.Visibility = System.Windows.Visibility.Visible;
                        image_Plan.Visibility = System.Windows.Visibility.Visible;
                        label_ModelInfo.Content = "".ToString();
                        label_PlanInfo.Content = "".ToString();
                        image_Model.Visibility = System.Windows.Visibility.Visible;
                        image_Plan.Visibility = System.Windows.Visibility.Visible;
                        // destFile = selectedPrj.proj_planImage;
                        image_Plan.Source = new BitmapImage(new Uri(destFile));


                        // modlDestFile = selectedPrj.proj_modelImage;
                        image_Model.Source = new BitmapImage(new Uri(modlDestFile));
                    }


                }
            }
            catch
            {



            }

            Data_ProjectCriterias ObjProjCri = new Data_ProjectCriterias();
            ObjProjCri.Proj_reference = label_ProjRef.Content.ToString();
            List<cecbData.ProjectCriteria> selctedMetixCritriaDetails = ObjProjctsFunc.GetPendingProjMetrixDetailByCode(ObjProjCri);


            foreach (ProjectCriteria oPrjCrytDetails in selctedMetixCritriaDetails)
            {

                if (oPrjCrytDetails.proj_matrix.ToString() == "Leopold")
                {
                    label_Matrix.Content = "Leopold";

                    wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Visible;
                    wrapPanel_Importnce.Visibility = System.Windows.Visibility.Visible;
                    wrapPanel_SimpleCritria.Visibility = System.Windows.Visibility.Hidden;

                    if (oPrjCrytDetails.crt_type.ToString() == "Magnitude")
                    {

                        comboBox_magnitudeCriteria.Text = oPrjCrytDetails.crt_range.ToString();
                    }



                    else
                    {
                        comboBox_impotncCriteria.Text = oPrjCrytDetails.crt_range.ToString();
                    }
                }
                else
                {

                    label_Matrix.Content = "Simple";
                    comboBox_simplCriteria.Text = oPrjCrytDetails.crt_range.ToString();
                    wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Hidden;
                    wrapPanel_Importnce.Visibility = System.Windows.Visibility.Hidden;
                    wrapPanel_SimpleCritria.Visibility = System.Windows.Visibility.Visible;
                }
            }










        }


        private void ShowData(string gridProjref)
        {

            List<Project_AreaGrids> ListPrjGridDtls = ObjProjctsFunc.GetGridDtlsByRef(gridProjref);

            listView1.Items.Clear();
            foreach (Project_AreaGrids oPrjTypeDtls in ListPrjGridDtls)
            {
                Data_ProjectGridLocation oPrjTypes = new Data_ProjectGridLocation();
                oPrjTypes.Loc_latitude = oPrjTypeDtls.loc_latitude.ToString();
                oPrjTypes.Loc_longitude = oPrjTypeDtls.loc_longitude.ToString();

                listView1.Items.Add(oPrjTypes);

            }


        }





        //private void comboBox_ProjRefernc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{

        //    Data_Projects ObjProj = new Data_Projects();
        //    ObjProj.Proj_reference = comboBox_ProjRefernc.SelectedValue.ToString();
        //    Projects selectedPrj = ObjProjctsFunc.GetPendingProjectDetailByCode(ObjProj);
        //    try
        //    {

        //        if (ObjProj.Proj_reference != "-1")
        //        {
        //            textBox_projname.Text = selectedPrj.proj_name;
        //            comboBox_ProjType.SelectedValue = selectedPrj.proj_type;
        //            textBox_Location.Text = selectedPrj.proj_location;
        //            datePicker_FromDate.Text = selectedPrj.proj_fromDate.ToString();
        //            datePicker_ToDate.Text = selectedPrj.proj_toDate.ToString();



        //            destFile = selectedPrj.proj_planImage;




        //            modlDestFile = selectedPrj.proj_modelImage;





        //            if (destFile == null && modlDestFile == null)
        //            {
        //                label_ModelInfo.Content = "Image not found".ToString();
        //                label_PlanInfo.Content = "Image not found".ToString();
        //                image_Model.Visibility = System.Windows.Visibility.Hidden;
        //                image_Plan.Visibility = System.Windows.Visibility.Hidden;



        //            }
        //            else if (destFile == null)
        //            {
        //                label_ModelInfo.Content = "".ToString();
        //                image_Plan.Visibility = System.Windows.Visibility.Hidden;
        //                label_PlanInfo.Content = "Image not found".ToString();
        //                image_Model.Visibility = System.Windows.Visibility.Visible;
        //                image_Model.Source = new BitmapImage(new Uri(modlDestFile));
        //            }
        //            else if (modlDestFile == null)
        //            {
        //                label_PlanInfo.Content = "".ToString();
        //                label_ModelInfo.Content = "Image not Found".ToString();
        //                image_Model.Visibility = System.Windows.Visibility.Hidden;
        //                image_Plan.Visibility = System.Windows.Visibility.Visible;
        //                image_Plan.Source = new BitmapImage(new Uri(destFile));


        //            }
        //            else
        //            {
        //                image_Model.Visibility = System.Windows.Visibility.Visible;
        //                image_Plan.Visibility = System.Windows.Visibility.Visible;
        //                label_ModelInfo.Content = "".ToString();
        //                label_PlanInfo.Content = "".ToString();
        //                image_Model.Visibility = System.Windows.Visibility.Visible;
        //                image_Plan.Visibility = System.Windows.Visibility.Visible;
        //                // destFile = selectedPrj.proj_planImage;
        //                image_Plan.Source = new BitmapImage(new Uri(destFile));


        //                // modlDestFile = selectedPrj.proj_modelImage;
        //                image_Model.Source = new BitmapImage(new Uri(modlDestFile));
        //            }


        //        }
        //    }
        //    catch
        //    {



        //    }



        //    Data_ProjectCriterias ObjProjCri = new Data_ProjectCriterias();
        //    ObjProjCri.Proj_reference = comboBox_ProjRefernc.SelectedValue.ToString();
        //    List<cecbData.ProjectCriteria> selctedMetixCritriaDetails = ObjProjctsFunc.GetPendingProjMetrixDetailByCode(ObjProjCri);


        //    foreach (ProjectCriteria oPrjCrytDetails in selctedMetixCritriaDetails)
        //    {

        //        if (oPrjCrytDetails.proj_matrix.ToString() == "Leopold")
        //        {
        //            label_Matrix.Content = "Leopold";

        //            wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Visible;
        //            wrapPanel_Importnce.Visibility = System.Windows.Visibility.Visible;
        //            wrapPanel_SimpleCritria.Visibility = System.Windows.Visibility.Hidden;

        //            if (oPrjCrytDetails.crt_type.ToString() == "Magnitude")
        //            {

        //                comboBox_magnitudeCriteria.Text = oPrjCrytDetails.crt_range.ToString();
        //            }



        //            else
        //            {
        //                comboBox_impotncCriteria.Text = oPrjCrytDetails.crt_range.ToString();
        //            }
        //        }
        //        else
        //        {

        //            label_Matrix.Content = "Simple";
        //            comboBox_simplCriteria.Text = oPrjCrytDetails.crt_range.ToString();
        //            wrapPanel_Magnitude.Visibility = System.Windows.Visibility.Hidden;
        //            wrapPanel_Importnce.Visibility = System.Windows.Visibility.Hidden;
        //            wrapPanel_SimpleCritria.Visibility = System.Windows.Visibility.Visible;
        //        }
        //    }






        //}

        private void button_Update_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Data_Projects ObjProjcts = new Data_Projects();
                ObjProjcts.Proj_reference = label_ProjRef.Content.ToString();
                ObjProjcts.Proj_fromDate = Convert.ToDateTime(datePicker_FromDate.Text);
                ObjProjcts.Proj_toDate = Convert.ToDateTime(datePicker_ToDate.Text);


                Data_ProjectCriterias ObjProjCitria = new Data_ProjectCriterias();
                Data_ImportCriterias ObjImortCritria = new Data_ImportCriterias();

                int Days = datePicker_ToDate.SelectedDate.Value.Subtract(datePicker_FromDate.SelectedDate.Value).Days;

                if (Days > 0)
                {
                    ObjProjctsFunc.UpdateEIADetails(ObjProjcts);

                    if (label_Matrix.Content == "Leopold")
                    {

                        ObjProjCitria.Proj_reference = label_ProjRef.Content.ToString();
                        ObjProjCitria.Crt_range = comboBox_impotncCriteria.Text;
                        ObjProjctsFunc.UpdateMagnitudeMetrixDetails(ObjProjCitria);

                        ObjImortCritria.Proj_reference = label_ProjRef.Content.ToString();
                        ObjImortCritria.Crt_range = comboBox_magnitudeCriteria.Text;
                        ObjProjctsFunc.UpdateImpotenceMetrixDetails(ObjImortCritria);


                    }
                    else
                    {
                        ObjProjCitria.Proj_reference = label_ProjRef.Content.ToString();
                        ObjProjCitria.Crt_range = comboBox_simplCriteria.Text;
                        ObjProjctsFunc.UpdateSimpleMetrixDetails(ObjProjCitria);

                    }
                    MessageBox.Show("Project Updated Successfully", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                }

                else
                {

                    MessageBox.Show("Project Update failed!", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

                }
            }

            catch
            {
                MessageBox.Show("Project insertion is faillled!", "Message", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void datePicker_ToDate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            int Days = datePicker_ToDate.SelectedDate.Value.Subtract(datePicker_FromDate.SelectedDate.Value).Days;

            if (datePicker_ToDate.SelectedDate.HasValue && Days > 0)
                label_Dates.Content = "No Of Days : " + Days.ToString();

            else
                label_Dates.Content = "Entered Time Duration is Inavlied!!!";
        }



    }
}
